package cn.aresoft.excel.utils;


//import com.spire.xls.*;


import com.spire.xls.ViewMode;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

public class WatermarkUtil {

    /**
     * 生成水印共用方法
     * @param bytes
     * @return
     */
    public static byte[] waterMarkGenerator(byte[] bytes,String markString){
        //加载Excel测试文档
        ByteArrayInputStream byteArrayInputStream =  new ByteArrayInputStream(bytes);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Workbook wb = new Workbook();
        wb.loadFromStream(byteArrayInputStream);
        //设置文本和字体大小
        Font font = new Font("仿宋", Font.PLAIN, 25);

        for (int i =0;i<wb.getWorksheets().getCount();i++)
        {
            Worksheet sheet = wb.getWorksheets().get(i);
            //调用DrawText() 方法插入图片
            BufferedImage imgWtrmrk = drawText(markString, font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());
            //将图片设置为页眉
            sheet.getPageSetup().setBackgoundImage(imgWtrmrk);
            //sheet.getPageSetup().setLeftHeader("&G");
            //将显示模式设置为Layout
            sheet.setViewMode(ViewMode.Normal);
        }
        //保存文档
        wb.saveToStream(out);
        return out.toByteArray();
    }

    private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)
    {
        //定义图片宽度和高度
        BufferedImage img=new BufferedImage((int)width,(int)height,TYPE_INT_ARGB);
        Graphics2D loGraphic=img.createGraphics();
        //获取文本size
        FontMetrics loFontMetrics=loGraphic.getFontMetrics(font);
        int liStrWidth=loFontMetrics.stringWidth(text);
        int liStrHeight=loFontMetrics.getHeight();
        /**设置透明度*/
        AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); // 创建透明度对象
        loGraphic.setComposite(alpha); // 设置透明度
        //文本显示样式及位置
        loGraphic.setColor(backColor);
        loGraphic.fillRect(0,0,(int)width,(int)height);
        loGraphic.translate(((int)width-liStrWidth)/2,((int)height-liStrHeight)/2);
        loGraphic.translate(-((int)width-liStrWidth)/2,-((int)height-liStrHeight)/2);
        loGraphic.setFont(font);
        loGraphic.setColor(textColor);
        /*loGraphic.rotate(Math.toRadians(30));
        loGraphic.drawString(text,((int)width-liStrWidth)/12+20,((int)height-liStrHeight)/12+50);
        loGraphic.drawString(text,((int)width-liStrWidth)/3+55,((int)height-liStrHeight)/3+50);
        loGraphic.drawString(text,((int)width-liStrWidth)/2+95,((int)height-liStrHeight)/2+100);*/
        loGraphic.rotate(Math.toRadians(40));
        loGraphic.drawString(text,((int)width-liStrWidth)/12+50,((int)height-liStrHeight)/12);
        loGraphic.drawString(text,((int)width-liStrWidth)/6+175,((int)height-liStrHeight)/4+50);
        loGraphic.drawString(text,((int)width-liStrWidth)/3+295,((int)height-liStrHeight)/2+50);
        loGraphic.drawString(text,((int)width-liStrWidth)+175,((int)height-liStrHeight)+50);
        loGraphic.dispose();
        return img;
    }
}
